/// *******
/// 评论控件
///
/// *******
import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';
import '../../app_files.dart';
import 'common_widget.dart';

class CommentModel {
  final String commentId;
  final String userHeader;
  final String userId;
  final String userName;
  final String content;
  final String time;
  final bool isUser;
  final String toID;

  ///是否是作者
  int favNum;
  bool isFav;
  List<CommentModel> replyList;

  bool showAllReply;

  ///回复人名称
  final bool isReply;
  final String replyToUserName;
  final String replyToUserId;

  ///回复人ID

  CommentModel({
    this.content,
    this.commentId,
    this.userId,
    this.userHeader,
    this.userName,
    this.time,
    this.isFav = false,
    this.favNum = 0,
    this.isUser = false,
    this.replyList,
    this.showAllReply = false,
    this.isReply = false,
    this.replyToUserId,
    this.replyToUserName,
    this.toID,
  });

  static fromJson(var json, {bool isReply = false}) {
    List reply = json['reply'];
    List<CommentModel> replyList = [];
    if (reply != null)
      replyList = reply.map<CommentModel>((var sub) {
        return CommentModel.fromJson(sub, isReply: true);
      }).toList();
    return CommentModel(
      commentId: CommonUtils.checkNullToString(json['id']),
      userId: CommonUtils.checkNullToString(json['memberID']),
      userHeader: CommonUtils.checkNullToString(json['headimg']),
      content: CommonUtils.checkNullToString(json['content']),
      userName: CommonUtils.checkNullToString(json['nickname']),
      favNum: int.tryParse(CommonUtils.checkNullToString(json['digg'])) ?? 0,
      isFav: CommonUtils.checkNullToString(json['isDigg']) == '1',
      isUser: CommonUtils.checkNullToString(json['memberID']) ==
          CommonUtils.checkNullToString(json['authorID']),
      time: CommonUtils.checkNullToString(json['createTime']),
      replyList: replyList,
      isReply: isReply,
      replyToUserName: CommonUtils.checkNullToString(json['toNickname']),
    );
  }
}

class CommentWidget extends StatelessWidget {
  final Function(CommentModel, CommentModel) onFav;
  final Function(String) onUserTap;
  final Function(CommentModel) onShowAll;
  final Function(CommentModel, CommentModel) onReplyTap;
  final double size;
  final bool hasBottom;
  final CommentModel model;

  const CommentWidget({
    this.onFav,
    this.onUserTap,
    this.onShowAll,
    this.onReplyTap,
    this.model,
    this.size = 28,
    this.hasBottom = true,
  });

  @override
  Widget build(BuildContext context) {
    int replyCount = 0;
    if (model.replyList == null || model.replyList.length == 0)
      replyCount = 0;
    else {
      if (model.replyList.length == 1)
        replyCount = 1;
      else {
        if (model.showAllReply)
          replyCount = model.replyList.length;
        else
          replyCount = 2;
      }
    }
    return Container(
      margin: EdgeInsets.only(
        left: 20,
        top: 10,
        bottom: hasBottom ? 10 : 0,
      ),
      child: Column(
        children: <Widget>[
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Expanded(
                child: Container(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Row(
                        children: <Widget>[
                          GestureDetector(
                            child: ClipRRect(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(size / 2)),
                              child: CustomImage(
                                model.userHeader,
                                width: size,
                                height: size,
                              ),
                            ),
                            onTap: () => onUserTap(model.userId),
                          ),
                          InkWell(
                            child: Padding(
                              padding: EdgeInsets.only(left: 6),
                              child: Text(
                                model.userName,
                                style: TextStyle(
                                    color: Color(0xFF666666), fontSize: 14),
                              ),
                            ),
                            onTap: () => onUserTap(model.userId),
                          ),
                          model.isUser
                              ? Container(
                                  margin: EdgeInsets.only(left: 6),
                                  width: 30,
                                  height: 16,
                                  decoration: BoxDecoration(
                                    border:
                                        Border.all(color: Color(0xFFD4D4D4)),
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(7)),
                                  ),
                                  child: Center(
                                    child: Padding(
                                      padding: EdgeInsets.only(bottom: 2),
                                      child: Text(
                                        '作者',
                                        style: TextStyle(
                                            color: Color(0xFFD4D4D4),
                                            fontSize: 10),
                                      ),
                                    ),
                                  ),
                                )
                              : Container(),
                        ],
                      ),
                      Padding(
                        padding:
                            EdgeInsets.only(top: 4, bottom: 4, left: size + 6),
                        child: model.isReply &&
                                model.replyToUserName.length != 0
                            ? GestureDetector(
                                child: RichText(
                                  text: TextSpan(
                                    children: [
                                      TextSpan(
                                          text: '回复 ',
                                          style: TextStyle(
                                            color: Color(0xFF222222),
                                            fontSize: 14,
                                          )),
                                      TextSpan(
                                          text: model.replyToUserName,
                                          style: TextStyle(
                                              color: Color(0xFF666666),
                                              fontSize: 14),
                                          recognizer: TapGestureRecognizer()
                                            ..onTap = () {
                                              onUserTap(model.replyToUserId);
                                            }),
                                      TextSpan(
                                          text: '：${model.content}',
                                          style: TextStyle(
                                              color: Color(0xFF222222),
                                              fontSize: 14)),
                                    ],
                                  ),
                                ),
                                onTap: () => onReplyTap(model, null),
                              )
                            : GestureDetector(
                                child: Text(
                                  model.content,
                                  style: TextStyle(
                                    color: Color(0xFF222222),
                                    fontSize: 14,
                                  ),
                                ),
                                onTap: () => onReplyTap(model, null),
                              ),
                      ),
                      Padding(
                        padding: EdgeInsets.only(left: size + 6),
                        child: Text(
                          model.time,
                          style:
                              TextStyle(color: Color(0xFF999999), fontSize: 11),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              InkWell(
                child: Container(
                  padding: EdgeInsets.only(left: 4),
                  color: Colors.white,
                  child: Column(
                    children: <Widget>[
                      Image.asset(
                        model.isFav
                            ? 'images/like_full.png'
                            : 'images/fav_icon.png',
                        width: 20,
                        height: 20,
                        fit: BoxFit.fill,
                      ),
                      Padding(
                        padding: EdgeInsets.only(top: 4),
                        child: Text(
                          model.favNum == 0 ? '' : '${model.favNum}',
                          style: TextStyle(
                            color: Color(0xFF666666),
                            fontSize: 11,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                onTap: () => onFav(model, null),
              ),
              Padding(padding: EdgeInsets.only(left: hasBottom ? 20 : 0)),
            ],
          ),
          Padding(
            padding: EdgeInsets.only(left: 10, right: 20),
            child: ListView.builder(
              itemBuilder: (BuildContext context, int index) {
                if (index == 1 && !model.showAllReply) {
                  return InkWell(
                    child: Padding(
                      padding: EdgeInsets.only(top: 6, left: size + 16),
                      child: Row(
                        children: <Widget>[
                          Text(
                            '展开${model.replyList.length - 1}条回复',
                            style: TextStyle(
                              color: Color(0xFF539BFF),
                              fontSize: 13,
                            ),
                          ),
                          Padding(padding: EdgeInsets.only(left: 4)),
                          Image.asset(
                            'images/blue_arrow.png',
                            height: 6,
                          ),
                        ],
                      ),
                    ),
                    onTap: () => onShowAll(model),
                  );
                }
                CommentModel item = model.replyList[index];
                return CommentWidget(
                  model: item,
                  size: 20,
                  hasBottom: false,
                  onUserTap: (String userId) {
                    onUserTap(userId);
                  },
                  onFav: (CommentModel item, CommentModel sub) {
                    onFav(model, item);
                  },
                  onReplyTap: (CommentModel item, CommentModel sub) {
                    onReplyTap(model, item);
                  },
                );
              },
              shrinkWrap: true,
              physics: new NeverScrollableScrollPhysics(),
              itemCount: replyCount,
            ),
          ),
          hasBottom
              ? Container(
                  margin: EdgeInsets.only(top: 10, left: size + 6),
                  height: 1,
                  color: Color(0xFFF8F8F8),
                )
              : Container(),
        ],
      ),
    );
  }
}
